package aufgabe2_pop3;

public class Server_Administration_Control implements Server_Administration_Control_I_for_Server_Administration, Server_Administration_Control_I_for_Server_Thread, Server_Administration_Control_I_for_Server{

	private static boolean server_main_run;
	
	private static int max_connections;
	
	private static int current_connections;
	
	private static int current_connections_wait;
	
	private static String pass;
	
	public Server_Administration_Control(int max_connections, String pass){
		
		Server_Administration_Control.max_connections = max_connections;
		
		Server_Administration_Control.current_connections = 0;
		
		Server_Administration_Control.pass = pass;
		
		Server_Administration_Control.server_main_run = true;
	}
	
	synchronized public boolean try_shutdown(String pass){
		
		if(Server_Administration_Control.pass.equals(pass)){
			server_main_run = false;
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	synchronized public void server_thread_counter_up() {
		current_connections++;
	}

	@Override
	synchronized public void server_thread_counter_down() {
		current_connections_wait--;
		current_connections--;
	}

	@Override
	synchronized public boolean is_place_for_threads() {
		if(current_connections_wait < max_connections){
			current_connections_wait++;
			return true;
		} else {
			return false;
		}
	}

	@Override
	synchronized public boolean server_run() {
		return server_main_run;
	}

	@Override
	synchronized public boolean server_threads_running() {
		if(current_connections > 0){
			return true;
		} else {
			return false;
		}
	}

	@Override
	public int get_running() {
		return current_connections;
	}
}
